﻿package sprites {
	import org.flixel.*;
	import org.flixel.data.*;

	/**
	 * ...
	 * @author test
	 */
	public class Fire extends FlxGroup {
		public var fires:Array;
		public var pre_limit:uint = 0;
		public var fires_loc:Array;
		public var delay:Number;
		public var spread_count:Number;

		public var on:Boolean;
		[Embed(source="../../bin/imgs/sets/fire-n-flames.png")]
		public var FireGraphic:Class;
		public var tilemap:FlxTilemap;
		public var origi_pos:FlxPoint;
		public var origi_tile_pos:FlxPoint;


		static public var HP:uint = 3;
		static public var BURN_TILES:Array = [3];

		static public var IS_FIRE:uint = 1;
		static public var NONE:uint = 0;

		override public function update():void {
			super.update();
			if (on){
				/*
				   spread_count -= FlxG.elapsed;
				   if (spread_count <= 0){
				   spread_count = delay;
				   spread();
				   }
				 */
				if (FlxG.mouse.justReleased()){
					spread();
				}

			}
		}

		/*
		 * 火焰擴張方向錯誤
		 * 火焰擴張方向錯誤
		 * 火焰擴張方向錯誤
		 * 火焰擴張方向錯誤
		 */

		public function Fire(_tilemap:FlxTilemap, _x:Number, _y:Number, _delay:Number = 3){
			super();
			fires = new Array();
			
			delay = _delay;
			spread_count = _delay;
			on = false;
			tilemap = _tilemap;

			origi_pos = new FlxPoint(_x, _y);
			origi_tile_pos = Utils.generalToTile(origi_pos.x, origi_pos.y);
			
			/*
			fires_loc = new Array();
			for (var i:uint = 0; i < tilemap.widthInTiles; i++){
				fires_loc[i] = new Array();
				for (var j:uint = 0; j < tilemap.heightInTiles; j++){
					fires_loc[i][j] = new uint(Fire.NONE);
				}
			}
			*/
		/*
		   var ff:FlxSprite = new FlxSprite();
		   ff.loadGraphic(FireGraphic, true);
		   ff.addAnimation("Burn", [1, 2, 3, 4], 5, true);
		   ff.play("Burn", true);
		   ff.scale = new FlxPoint(0.35, 0.35);
		   add(ff);
		 */
		}

		public function turnOn():void {
			addFire(origi_tile_pos.x, origi_tile_pos.y);
			on = true;
		}

		public function spread():void {
			trace("===START SPREADING===");
			var dir:Array = [[0, 1], [1, 0], [-1, 0], [0, -1] /*, [1, 1], [1, -1], [-1, 1], [-1, -1]*/];
			var limit:uint = fires.length;
			trace("limit", limit);
			for (var index:uint = pre_limit; index < limit; index++){
				var f:FlxSprite = fires[index];
				if (f.exists) {
					var tile_loc:FlxPoint = Utils.generalToTile(f.x, f.y);
					trace("tile_loc", tile_loc.x, tile_loc.y);
					for (var i:String in dir){
						var n_loc:FlxPoint = new FlxPoint(tile_loc.x + dir[i][0], tile_loc.y + dir[i][1]);
						if (n_loc.x >= 0 && n_loc.y >= 0 && n_loc.x < tilemap.widthInTiles && n_loc.y < tilemap.heightInTiles){
							for each (var tid:uint in BURN_TILES){
								if (tilemap.getTile(n_loc.x, n_loc.y) == tid){
									trace("XDD", n_loc.x, n_loc.y);
									if (fires_loc[n_loc.x][n_loc.y] == NONE){
										//		trace("", n_loc.x, n_loc.y, tilemap.getTile(n_loc.x, n_loc.y), tid);
										addFire(n_loc.x, n_loc.y);
									}
										//FlxG.pause = true;
								}
							}
						}
					}
				}
			}
			pre_limit = limit - 1;
		}

		public function addFire(_x:uint, _y:uint):void {
			trace("addFire", _x, _y);

			/*
			 * Tile Add CallBack{ if(obj is bullet) obj.kill(); }
			 */

			var f:FlxSprite = new FlxSprite();
			f.loadGraphic(FireGraphic, true);
			//f.origin = new FlxPoint(0, 0);
			f.addAnimation("Burn", [1, 2, 3, 4], 5, true);
			f.play("Burn", true)
			f.scale = new FlxPoint(1.5, 1.5);
			var pos:FlxPoint = Utils.tileToGeneral(_x, _y);
			f.x = pos.x // - f.frameWidth / 2;
			f.y = pos.y - f.frameHeight / 4;

			var off:Number = 1;
			f.width += off
			f.height += off;
			f.offset = new FlxPoint(-off / 2, -off / 2);

			f.health = HP;

			fires_loc[_x][_y] = IS_FIRE;
			
			var index:uint = fires.push(f);
			this.add(fires[index - 1]);
		}



	}

}